// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-06 09:45:13
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-11-06 10:01:45
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Load/CONVParser.cpp
 */

#include "Import/BDFImport/BulkData/Load/CONVParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/ICONVService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (CONV);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/CONV/CONV.md>
*/
// 以下需要修改
enum PCONVField : std::uint8_t
{
    PCONVFIELD_EID     = 2,   // 表面单元识别编号
    PCONVFIELD_PCONID  = 3,   // PCONV条目的对流属性识别编号
    PCONVFIELD_FLMND   = 4,   // 液膜对流流体属性温度的点
    PCONVFIELD_CNTRLND = 5,   // 自由对流边界条件的控制点
    PCONVFIELD_TA1     = 6,   //  用于对流的环境点
    PCONVFIELD_TA2     = 7,   //
    PCONVFIELD_TA3     = 8,   //
    PCONVFIELD_TA4     = 9,   //
    PCONVFIELD_TA5     = 12,  //
    PCONVFIELD_TA6     = 13,  //
    PCONVFIELD_TA7     = 14,  //
    PCONVFIELD_TA8     = 15   //
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCONV (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                            std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存CONV数据的临时结构体对象
    SG::DataStructure::FEM::CONVData tempCONV;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempCONV.m_domainId = 1;

    // 将输入的条目信息分割成单个词条

    // 初始化当前处理的字符串条目的ID
    int curIDKey = 1;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            // 如果当前条目为空，跳过当前循环并处理下一个条目
            curIDKey++;
            continue;
        }

                // 根据条目ID，将解析后的数值存入对应的CONV字段
        switch (curIDKey)
        {
        case PCONVFIELD_EID:
            tempCONV.m_id = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_PCONID:
            tempCONV.m_PCONID = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_FLMND:
            tempCONV.m_FLMND = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_CNTRLND:
            tempCONV.m_CNTRLND = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_TA1:
            tempCONV.m_TA[0] = convertToI64 (curStrKey);
            tempCONV.m_TA[1] = tempCONV.m_TA[2] = tempCONV.m_TA[3] = tempCONV.m_TA[4] = tempCONV.m_TA[5] = tempCONV.m_TA[6] =
                tempCONV.m_TA[7]                                                                         = tempCONV.m_TA[0];
            break;
        case PCONVFIELD_TA2:
            tempCONV.m_TA[1] = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_TA3:
            tempCONV.m_TA[2] = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_TA4:
            tempCONV.m_TA[3] = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_TA5:
            tempCONV.m_TA[4] = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_TA6:
            tempCONV.m_TA[5] = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_TA7:
            tempCONV.m_TA[6] = convertToI64 (curStrKey);
            break;
        case PCONVFIELD_TA8:
            tempCONV.m_TA[7] = convertToI64 (curStrKey);
            break;
        default:
            break;
        }

        // 增加条目ID，以便处理下一个条目
        curIDKey++;
    }

    // 将解析后的CONV数据通过数据库服务工厂的CONV服务添加到数据库中
    return dbServiceFactory->get<ICONVService> ()->Add (tempCONV);
}